package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUesrInfoCacheService;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

/**
 * @author 猫教授
 * @date 2019/8/7 19:19
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUesrInfoCacheService redisCacheService;

    @Override
    public void deleteByPrimaryKey(Long id) {

    }

    @Override
    public void insert(UserInfo record) {

    }

    @Override
    public UserInfo selectByPrimaryKey(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserInfo> selectAll() {
        return userInfoMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(UserInfo record) {
        userInfoMapper.updateByPrimaryKey(record);

    }

    @Override
    public boolean checkPhoneByphone(String phone) {

        return userInfoMapper.checkPhoneByphone(phone) > 0;
    }

    @Override
    public void sendVerifyCode(String phone) throws LogicException {
        //首先要对手机号码进行验证
        //这里自己写一个验证断言工具类
        //验证手机号码是否为空
        AssertUtil.haslength(phone, "手机号不能为空");
        //验证手机号码是否已经注册过了
        if (userInfoMapper.checkPhoneByphone(phone) > 0) {
            throw new LogicException("手机号已经注册过了");
        }
        //验证通过后获得验证码
        String VerifyCode = UUID.randomUUID().toString().substring(0, 4);
        StringBuffer buffer = new StringBuffer(50);
        StringBuffer sb = buffer.append("您好,验证信息为").append(VerifyCode).append(",请在5分钟内输入,如不是您本人操作请忽略");
        System.out.println(sb);
        //发送验证码
        //如果发送失败即抛出异常
        //发送成功后,把手机号码和验证码存入redis以便信息共享
        redisCacheService.setVerifyCode(phone, VerifyCode);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        AssertUtil.haslength(phone, "手机号不能为空");
        AssertUtil.haslength(nickname, "用户明不能为空");
        AssertUtil.haslength(password, "密码不能为空");
        AssertUtil.haslength(rpassword, "密码验证不能为空");
        AssertUtil.haslength(verifyCode, "验证码不能为空");
        AssertUtil.comparison(password, rpassword, "密码前后不一");
        String Code = redisCacheService.getVerifyCode(phone);
        if (Code == null) {
            throw new LogicException("手机号输入错误,或验证码已过期");
        }
        AssertUtil.comparison(Code, verifyCode, "验证码输入错误,请重新输入");
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        //这里需要对密码进行加密,使用spring框架自带的加密工具类DigestUtils.md5DigestAsHex
        //这里的盐选用用户名
        password=password+nickname;
        userInfo.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) {
        //首先先进行账号密码的校验
        AssertUtil.haslength(username,"账号不能为空");
        AssertUtil.haslength(password,"密码不能为空");
        //首先在数据库进行信息验证
        UserInfo userInfo=userInfoMapper.userLoginCheckUsernameAndPawssword(username,password);
        if (userInfo==null){
            throw  new LogicException("账号或密码有误,请重新登录");
        }
        //这里需要存入redis缓存了
        //这里只需要用到存入,但是我们为后面的需求所以我们连同那个取出也一起做了,然后再取出信息里面顺带把续约也做了
        //这里我们需要存入用户信息,其他信息由缓存业务层处理
        String token=redisCacheService.setUserInfo(userInfo);
        //存入成功后把信息传回前台cookie
        return token;
    }

    //修改头像
    @Override
    public void saveOrUpdateheadImgUrl(String s, UserInfo userInfo) {
        userInfo.setHeadImgUrl(s);
        userInfoMapper.updateheadImg(userInfo);

    }

    //修改密码
    @Override
    public void updatePasswordOrPhoneById(UserInfo userInfo) {
        userInfo.setPhone(userInfo.getPhone());
        userInfo.setPassword(userInfo.getPassword());


        userInfoMapper.updatePasswordOrPhoneById(userInfo);
    }

    //黑名单
    @Override
    public UserInfo selectBadUser(String nickname) {
        UserInfo userInfo =  userInfoMapper.selectBadUser(nickname);
        return userInfo;
    }



    /*@Override
    public void updateState(UserInfo userInfo) {
        userInfoMapper.updateState(userInfo);
    }*/

    //加钱
    @Override
    public void addCoin(int coin, Long uid) {

        UserInfo user = userInfoMapper.selectByPrimaryKey(uid);

        user.setCoin(user.getCoin() + coin);
        int coins = user.getCoin();

        userInfoMapper.addCoin(coins,uid);
    }

    //减钱
    @Override
    public void subCoin(int coin, Long uid) {
        UserInfo user = userInfoMapper.selectByPrimaryKey(uid);

        user.setCoin(user.getCoin() - coin);
        int coins = user.getCoin();


        userInfoMapper.subCoin(coins,uid);

    }


}
